package hardwarelvl;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.rms.*;

public class ScheduleStorage {

	private static final String m_storeName = "scheduler_store";
	private RecordStore m_storage;

	public ScheduleStorage() {
	}
	
	public void open() throws Throwable{
		try {
			m_storage = RecordStore.openRecordStore(m_storeName, true);
		}
		catch (RecordStoreFullException e) { 
			throw new Exception("open: RecordStoreFullException " + e.getMessage());
		}
		catch (RecordStoreNotOpenException e) { 
			throw new Exception("open: RecordStoreNotOpenException" + e.getMessage());
		}
		catch (RecordStoreException e) { 
			throw new Exception("open: RecordStoreException" + e.getMessage());
		} catch (Throwable e) {
			throw e;
		} 
	}
	
	public Hashtable readAll() throws Throwable {
		try{
			Hashtable records = new Hashtable();
			
			RecordEnumeration re = m_storage.enumerateRecords(null, null, false);
			while (re.hasNextElement()) {
				int recordID = re.nextRecordId();
				byte data[] = new byte[m_storage.getRecordSize(recordID)];
				m_storage.getRecord(recordID, data, 0);
				String xml = new String(data);
				records.put(new Integer(recordID), xml);
			}
			return records;
		}
		catch (Exception e) { 
			throw new Exception("Can't read data " + e.getMessage());
		}
	}
	
	public boolean isEmpty() throws Throwable{
		try {
			return m_storage.getNumRecords() == 0;
		} 
		catch (RecordStoreNotOpenException e) {
			throw new Exception("RecordStoreNotOpenException:"+e.getMessage());
		}
	}
	
	public int saveEntry(String entry) throws Throwable{
		try{		
			byte data[] = entry.getBytes();
			return m_storage.addRecord(data, 0, data.length);
		}
		catch(RecordStoreException rse){
			throw new Exception("save: RecordStoreException:"+rse.getMessage());
		}
	}
	
	public void close() throws Throwable{
		try{
			m_storage.closeRecordStore();
		}
		catch(RecordStoreException rse){
			throw new Exception(rse.getMessage());
		}
	}
	
	public void deleteEntry(int entryId) throws Throwable{
		try{
			m_storage.deleteRecord(entryId);
		} 
		catch (RecordStoreNotOpenException e) {
			throw new Exception("RecordStoreNotOpenException:"+e.getMessage());
		}
		catch (RecordStoreException e) {
			throw new Exception("RecordStoreException:"+e.getMessage());
		}
	}

	public void deleteAll() throws Throwable{
		Vector recordIds = new Vector();
		try {
			RecordEnumeration re = m_storage.enumerateRecords(null, null, false);
			while (re.hasNextElement()) {
				recordIds.addElement(new Integer(re.nextRecordId()));
			}

			Enumeration keys = recordIds.elements(); 
			while (keys.hasMoreElements()){
				Integer key = (Integer)(keys.nextElement());
				m_storage.deleteRecord(key.intValue());
			}
		} 
		catch (RecordStoreNotOpenException e) {
			throw new Exception("RecordStoreNotOpenException:"+e.getMessage());
		}
		catch (RecordStoreException e) {
			throw new Exception("RecordStoreException:"+e.getMessage());
		}
	}
	
}
